package com.tanglinkai.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanglinkai.mall.contstants.CategoryErrorMessages;
import com.tanglinkai.mall.entity.po.Category;
import com.tanglinkai.mall.excpetion.BusinessException;
import com.tanglinkai.mall.mapper.CategoryMapper;
import com.tanglinkai.mall.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public void addCategory(Category category) {
        if (category.getName() == null || category.getName().trim().isEmpty()) {
            throw new IllegalArgumentException(CategoryErrorMessages.CATEGORY_NAME_EMPTY);
        }
        this.save(category);
    }

    /**
     * 获取所有分类
     * @return
     */
    @Override
    public List<Category> getAllCategories() {
        return this.list();
    }

    /**
     *根据id查询分类
     * @param id
     * @return
     */

    @Override
    public Category getCategoryById(Integer id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException(CategoryErrorMessages.CATEGORY_INFO_INCOMPLETE);
        }
        return this.getById(id);
    }

    /**
     * 修改分类
     * @param category
     * @throws IllegalArgumentException
     */
    @Override
    public void updateCategory(Category category) throws IllegalArgumentException {
        // 1. 参数校验
        if (category == null || category.getId() == null) {
            throw new IllegalArgumentException(CategoryErrorMessages.CATEGORY_INFO_INCOMPLETE);
        }

        if (category.getName() == null || category.getName().trim().isEmpty()) {
            throw new IllegalArgumentException(CategoryErrorMessages.CATEGORY_NAME_EMPTY);
        }

        // 2. 检查分类是否存在
        Category existing = this.getById(category.getId());
        if (existing == null) {
            throw new IllegalArgumentException(CategoryErrorMessages.CATEGORY_NOT_FOUND);
        }

        // 3. 检查名称是否重复
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName, category.getName())
                .ne(Category::getId, category.getId());
        if (this.count(queryWrapper) > 0) {
            throw new IllegalArgumentException(CategoryErrorMessages.CATEGORY_NAME_EXISTS);
        }

        // 4. 更新分类
        this.updateById(category);
    }

    /**
     * 删除分类
     * @param id
     * @throws BusinessException
     */
    @Override
    public void deleteCategory(Integer id) throws BusinessException {
        // 1. 参数校验
        if (id == null || id <= 0) {
            throw new IllegalArgumentException(CategoryErrorMessages.CATEGORY_ID_INVALID);
        }

        // 2. 检查分类是否存在
        Category category = this.getById(id);
        if (category == null) {
            throw new BusinessException(CategoryErrorMessages.CATEGORY_NOT_FOUND);
        }
        // 3. 执行删除
        this.removeById(id);
    }

}